<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
var mergeTwoLists = function(l1, l2) {
    let p1 = l1
    let p2 = l2
    let l3 = new ListNode()
    let p3 = l3
    while(p1 || p2){
        if(p1 && p2 && p1.val <= p2.val){
            p3.next = new ListNode(p1.val)
            if(p1){
                p1 = p1.next
            }
        }
        else if(p1 && p2 && p1.val > p2.val){
            p3.next = new ListNode(p2.val)
            if(p2){
                p2 = p2.next
            }
        }
        else {
            if(p1){
                while(p1){
                    p3.next = new ListNode(p1.val)
                    p3 = p3.next
                    p1 = p1.next
                }
            }
            if(p2){
                while(p2){
                    p3.next = new ListNode(p2.val)
                    p3 = p3.next
                    p2 = p2.next
                }
            }
        }
        p3 = p3.next
    }
    return l3.next
};

var mergeTwoLists = function(l1, l2) {
    let p1 = l1
    let p2 = l2
    let l3 = new ListNode()
    let p3 = l3
    while(p1 && p2){
        if(p1 && p2 && p1.val <= p2.val){
            p3.next = new ListNode(p1.val)
            if(p1){
                p1 = p1.next
            }
        }
        else if(p1 && p2 && p1.val > p2.val){
            p3.next = new ListNode(p2.val)
            if(p2){
                p2 = p2.next
            }
        }
        p3 = p3.next
    }
    if(p1) p3.next = p1
    if(p2) p3.next = p2
    return l3.next
};
    </script>
</body>
</html>